package cn.ac.jaas.isc.vegetable.pbms.service.impl;

import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.security.service.VegetableUser;
import cn.ac.jaas.isc.vegetable.common.security.util.SecurityUtils;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcEquipmentHistory;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcEquipmentHistoryMapper;
import cn.hutool.extra.servlet.ServletUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcEquipment;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcEquipmentMapper;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcEquipmentService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 设备业务层
 *
 * @author 张高昌
 * @date 2022-05-02 11:14:42
 */
@Service
@AllArgsConstructor
public class ArcEquipmentServiceImpl extends ServiceImpl<ArcEquipmentMapper, ArcEquipment> implements ArcEquipmentService {

    private final ArcEquipmentHistoryMapper arcEquipmentHistoryMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R saveEquipment(ArcEquipment arcEquipment, HttpServletRequest request) {
        LambdaQueryWrapper<ArcEquipment> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ArcEquipment::getEquipmentName, arcEquipment.getEquipmentName())
                .eq(ArcEquipment::getHouseId, arcEquipment.getHouseId())
                .eq(ArcEquipment::getEquipmentTypeCode, arcEquipment.getEquipmentTypeCode());
        ArcEquipment nameModel = baseMapper.selectOne(wrapper);
        if (nameModel != null) {
            return R.failed("设备名称已存在！");
        }
        LambdaQueryWrapper<ArcEquipment> ecWrapper = Wrappers.lambdaQuery();
        ecWrapper.eq(ArcEquipment::getEquipmentCode, arcEquipment.getEquipmentCode());
        ArcEquipment ecModel = baseMapper.selectOne(ecWrapper);
        if (ecModel != null) {
            return R.failed("设备编码已存在！");
        }

        VegetableUser user = SecurityUtils.getUser();
        arcEquipment.setCreateTime(LocalDateTime.now());
        arcEquipment.setCreateUser(user.getId());
        baseMapper.insert(arcEquipment);
        this.addEquipmentHistory(arcEquipment, request);
        return R.ok(true);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R updateEquipmentById(ArcEquipment arcEquipment, HttpServletRequest request) {
        LambdaQueryWrapper<ArcEquipment> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ArcEquipment::getEquipmentName, arcEquipment.getEquipmentName())
                .eq(ArcEquipment::getHouseId, arcEquipment.getHouseId())
                .eq(ArcEquipment::getEquipmentTypeCode, arcEquipment.getEquipmentTypeCode())
                .ne(ArcEquipment::getEquipmentId, arcEquipment.getEquipmentId());
        ArcEquipment nameModel = baseMapper.selectOne(wrapper);
        if (nameModel != null) {
            return R.failed("设备名称已存在！");
        }
        LambdaQueryWrapper<ArcEquipment> ecWrapper = Wrappers.lambdaQuery();
        ecWrapper.eq(ArcEquipment::getEquipmentCode, arcEquipment.getEquipmentCode())
                .ne(ArcEquipment::getEquipmentId, arcEquipment.getEquipmentId());
        ArcEquipment ecModel = baseMapper.selectOne(ecWrapper);
        if (ecModel != null) {
            return R.failed("设备编码已存在！");
        }
        VegetableUser user = SecurityUtils.getUser();
        arcEquipment.setUpdateTime(LocalDateTime.now());
        arcEquipment.setUpdateUser(user.getId());
        baseMapper.updateById(arcEquipment);
        this.addEquipmentHistory(arcEquipment, request);
        return R.ok(true);
    }

    /**
     * 添加设备历史操作记录
     *
     * @param arcEquipment
     * @param request
     */
    private void addEquipmentHistory(ArcEquipment arcEquipment, HttpServletRequest request) {
        ArcEquipmentHistory arcEquipmentHistory = new ArcEquipmentHistory();
        arcEquipmentHistory.setIp(ServletUtil.getClientIP(request));
        BeanUtils.copyProperties(arcEquipment, arcEquipmentHistory);
        arcEquipmentHistoryMapper.insert(arcEquipmentHistory);
    }

    @Override
    public Page getHistoryPage(Page page, Integer equipmentId) {
        List<ArcEquipmentHistory> pageList = arcEquipmentHistoryMapper.selectEquipmentHistory(page, equipmentId);
        return page.setRecords(pageList);
    }
}
